# 加载数据
import pandas as pd

data_train = pd.read_csv('T-R-train.csv')
data_test = pd.read_csv('T-R-test.csv')
x_train = data_train.loc[:, 'T']
y_train = data_train.loc[:, 'rate']
x_test = data_test.loc[:, 'T']
y_test = data_test.loc[:, 'rate']

# 可视化数据
from matplotlib import pyplot as plt

fig1 = plt.figure(figsize=(5, 5))

plt.scatter(x_train, y_train)
plt.title('T-R-train')
plt.xlabel('温度T', fontproperties='SimHei', fontsize=20)
plt.ylabel('反应速率R', fontproperties='SimHei', fontsize=20)
# plt.show()

# 数据维度转换
import numpy as np

x_train = np.array(x_train).reshape(-1, 1)
x_test = np.array(x_test).reshape(-1, 1)

# 进行线性回归模型预测
from sklearn.linear_model import LinearRegression

lr1 = LinearRegression()
lr1.fit(x_train, y_train)
# 进行预测
y_train_predict = lr1.predict(x_train)
y_test_predict = lr1.predict(x_test)

# 评估模型R2分数
from sklearn.metrics import r2_score

print('线性回归模型-训练数据的R2分数：', r2_score(y_train, y_train_predict))
print('线性回归模型-测试数据的R2分数：', r2_score(y_test, y_test_predict))

# 可视化模型预测结果
# 生成新的数据点
x_range = np.linspace(40,90,300).reshape(-1,1)
y_range_predict = lr1.predict(x_range)
plt.plot(x_range, y_range_predict,color='r')
# plt.show()

# 使用多项式模型进行预测
from sklearn.preprocessing import PolynomialFeatures
poly2 = PolynomialFeatures(degree=2)# 二阶
x_2_train = poly2.fit_transform(x_train)
x_2_test = poly2.transform(x_test)
lr2 = LinearRegression()
lr2.fit(x_2_train, y_train)
# 进行预测
y_2_train_predict = lr2.predict(x_2_train)
y_2_test_predict = lr2.predict(x_2_test)
# 评估模型R2分数
print('2阶线性回归模型-训练数据的R2分数：', r2_score(y_train, y_2_train_predict))
print('2阶线性回归模型-测试数据的R2分数：', r2_score(y_test, y_2_test_predict))
# 可视化
# 生成新的数据点
x_2_range = np.linspace(40,90,300).reshape(-1,1)
x_2_range = poly2.transform(x_2_range)
y_2_range_predict = lr2.predict(x_2_range)
plt.plot(x_range, y_2_range_predict,color='g')
# plt.show()

# 尝试5阶
poly5 = PolynomialFeatures(degree=5)# 五阶
x_5_train = poly5.fit_transform(x_train)
x_5_test = poly5.transform(x_test)
lr5 = LinearRegression()
lr5.fit(x_5_train, y_train)
# 进行预测
y_5_train_predict = lr5.predict(x_5_train)
y_5_test_predict = lr5.predict(x_5_test)
# 评估模型R2分数
print('5阶线性回归模型-训练数据的R2分数：', r2_score(y_train, y_5_train_predict))
print('5阶线性回归模型-测试数据的R2分数：', r2_score(y_test, y_5_test_predict))
# 可视化
# 生成新的数据点
x_5_range = np.linspace(40,90,300).reshape(-1,1)
x_5_range = poly5.transform(x_5_range)
y_5_range_predict = lr5.predict(x_5_range)
plt.plot(x_range, y_5_range_predict,color='orange')
# plt.show()

# 挑战极限
poly30 = PolynomialFeatures(degree=30)# 30阶
x_30_train = poly30.fit_transform(x_train)
x_30_test = poly30.transform(x_test)
lr30 = LinearRegression()
lr30.fit(x_30_train, y_train)
# 进行预测
y_30_train_predict = lr30.predict(x_30_train)
y_30_test_predict = lr30.predict(x_30_test)
# 评估模型R2分数
print('30阶线性回归模型-训练数据的R2分数：', r2_score(y_train, y_30_train_predict))
print('30阶线性回归模型-测试数据的R2分数：', r2_score(y_test, y_30_test_predict))
# 可视化
# 生成新的数据点
x_30_range = np.linspace(40,90,300).reshape(-1,1)
x_30_range = poly30.transform(x_30_range)
y_30_range_predict = lr30.predict(x_30_range)
plt.plot(x_range, y_30_range_predict,color='black')
plt.show()
